<template>
  <div>
    <div v-for="product in products" :key="product.id">
      <p>{{ product.name }} - {{ product.price }}元 - 数量: {{ product.quantity }}</p>
      <button @click="addToCart(product.id)">加入购物车</button>
      <button @click="removeFromCart(product.id)">移出购物车</button>
    </div>
    <p>总价：{{ total }}元</p>
  </div>
</template>

<script>
export default {
  computed: {
    products() {
      return this.$store.state.products
    },
    total() {
      return this.$store.getters.total
    }
  },
  methods: {
    addToCart(id) {
      this.$store.dispatch('addToCart', id)
    },
    removeFromCart(id) {
      this.$store.dispatch('removeFromCart', id)
    }
  }
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
}

nav {
  padding: 30px;
}

nav a {
  font-weight: bold;
  color: #2c3e50;
}

nav a.router-link-exact-active {
  color: #42b983;
}
</style>
